package vo.community.strategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import vo.community.CommunityVO;

/**
 * The Class CommunityUniverseStrategy.
 */
public class CommunityUniverseStrategy implements CommunityStrategy
{
  
  /** ma collection d'enfants. */
  private List<CommunityVO> _children;
  
  /** mon dico d'enfants key:category. */
  private Map<String, CommunityVO> _childrenDic;
  
  /**
   * Instantiates a new community universe strategy.
   */
  public CommunityUniverseStrategy()
  {
  }
  
  /**
   * @see vo.community.strategy.CommunityStrategy#addChild(vo.community.CommunityVO)
   */
  public void addChild(CommunityVO pCommunity)
  {
    if(pCommunity != null)
    {
      if(pCommunity.isCategory())
      {
        if(getChildrenDic().get(pCommunity.getCategoryCode()) == null)
        {
          getChildrenDic().put(pCommunity.getCategoryCode(),pCommunity);
          getChildren().add(pCommunity);
        }
        else
        {
          removeChild(pCommunity);
          addChild(pCommunity);
        }
      }
      else
      {
        if(pCommunity.getCategoryCode() != null)
        {
          CommunityVO myChild = getChildrenDic().get(pCommunity.getCategoryCode());
          
          if(myChild != null)
          {
            myChild.addChild(pCommunity);
          }
        }
      }
    }
  }
  
  /**
   * @see vo.community.strategy.CommunityStrategy#removeChild(vo.community.CommunityVO)
   */
  public void removeChild(CommunityVO pCommunity)
  {
    if(pCommunity != null)
    {
      if(pCommunity.isCategory() && getChildrenDic().get(pCommunity.getCategoryCode()) != null)
      {
        getChildren().remove(getChildrenDic().get(pCommunity.getCategoryCode()));
        getChildrenDic().remove(pCommunity.getCategoryCode());
      }
      else
      {
        if(pCommunity.getCategoryCode() != null)
        {
          CommunityVO myChild = getChildrenDic().get(pCommunity.getCategoryCode());
          
          if(myChild != null)
          {
            myChild.removeChild(pCommunity);
          }
        }
      }
    }
  }
  
  /**
   * @see vo.community.strategy.CommunityStrategy#getChildren()
   */
  public List<CommunityVO> getChildren()
  {
    if(_children == null)
    {
      _children = new ArrayList<CommunityVO>();
    }
    
    return _children;
  }
  
  /**
   * @see vo.community.strategy.CommunityStrategy#getChildrenDic()
   */
  public Map<String, CommunityVO> getChildrenDic()
  {
    if(_childrenDic == null)
    {
      _childrenDic = new HashMap<String, CommunityVO>();
    }
    return _childrenDic;
  }
}
